//
//  PKBaseViewController.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/14.
//


import UIKit
import RxSwift
import RxCocoa
import SnapKit
import Kingfisher
import NVActivityIndicatorView

class PKBaseViewController: UIViewController  ,NVActivityIndicatorViewable  {

//    var showBackButton: Bool {  true }
//    var shouldHideNavigationBar: Bool { false }
    // MARK: - Public
//    let disposeBag = DisposeBag()

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setuplayout()
        bindViewModel()
        bindEvents()
        logLifecycle("viewDidLoad")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        logLifecycle("viewWillAppear")
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        logLifecycle("viewDidDisappear")
    }

    // MARK: - Setup Methods (For Subclasses)

    func setuplayout(){
        
    }
    
    func setupUI() {
        view.backgroundColor = .systemBackground
    }

    func bindViewModel() {
        // 子类实现绑定逻辑
    }

    func bindEvents() {
        // 额外事件绑定，如按钮点击等
    }

    // MARK: - Navigation

    func setNavTitle(_ title: String) {
        navigationItem.title = title
    }

    // MARK: - HUD

    private var loadingView: NVActivityIndicatorView?

    func showLoading(mesage:String = "Loding...") {
//        if loadingView == nil {
//            let indicator = UIActivityIndicatorView(style: .large)
//            let indicator = NVActivityIndicatorView(frame: .init(x: 100, y: 300, width: 60, height: 60),type: .ballPulse ,color: .white,padding: 0)
////            view.addSubview(indicator)
////            indicator.backgroundColor = .yellow
//            AppContext.shared.currentWindow?.addSubview(indicator)
//            indicator.snp.makeConstraints { make in
//                make.center.equalToSuperview()
//            }
//            loadingView = indicator
//        }
//        loadingView?.startAnimating()
        startAnimating(CGSize(width: 30, height: 30), message: mesage,type: .squareSpin,color: .pinky,textColor: .amber, fadeInAnimation: nil)
//        loadingView?.startAnimating(size)
    }

    func SetAnotherMessage(message : String = "Authenticating...") {
        startAnimating(CGSize(width: 30, height: 30), message: "Loding...",type: .squareSpin,color: .pinky,textColor: .amber, fadeInAnimation: nil)
        NVActivityIndicatorPresenter.sharedInstance.setMessage(message)
    }
    
    
    func hideLoading() {
        self.stopAnimating()
    }

    // MARK: - Error Prompt

    func showError(_ message: String, title: String = "错误") {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(.init(title: "确定", style: .default))
        present(alert, animated: true)
    }

    // MARK: - Logging

    private func logLifecycle(_ function: String) {
        print("[\(type(of: self))] \(function)")
    }
}
